package br.tiagohm.markdownview;

import ohos.app.Context;
import ohos.global.resource.RawFileEntry;
import ohos.global.resource.Resource;
import ohos.global.resource.ResourceManager;

import java.io.*;
import java.lang.reflect.Array;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.Collection;
import java.util.List;
import java.util.Map;

public class Utils {
    private Utils() {
    }

    public static String getStringFromInputStream(InputStream is)
            throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int length;
        while ((length = is.read(buffer)) != -1) {
            baos.write(buffer, 0, length);
        }
        return baos.toString("UTF-8");
    }

    public static String getStringFromAssetFile(ResourceManager asset, String filename) {
        String json = null;
        RawFileEntry entry = asset.getRawFileEntry("resources/rawfile/"+filename);//"resources/rawfile/sample.json"
        try {
            Resource res = entry.openRawFile();
            int size = res.available();
            byte[] buffer = new byte[size];
            res.read(buffer);
            res.close();
            json = new String(buffer, StandardCharsets.UTF_8);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return json;
    }

    public static String getStringFromFile(File file) {
        InputStream is = null;

        try {
            is = new FileInputStream(file);
            return getStringFromInputStream(is);
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     *
     * 判断对象是否为空
     * @param obj 对象
     * @return 为空
     */
    public static boolean isEmpty(Object obj) {
        boolean flag = false;
        if (obj == null) return true;
        if(isEmptyToObj(obj)) flag = true;
        if(isEmptyToList(obj)) flag = true;
        return flag;

    }

    /**
     * isEmptyToObj
     *
     * @param obj
     * @return boolean
     */
    public static boolean isEmptyToObj(Object obj){
        if (obj instanceof Collection && ((Collection) obj).isEmpty()
                || obj instanceof String && obj.toString().length() == 0){
            return true;
        }

        return false;
    }

    /**
     * isEmptyToList
     *
     * @param obj
     * @return boolean
     */
    public static boolean isEmptyToList(Object obj){
        boolean flag = false;

        if (obj.getClass().isArray() && Array.getLength(obj) == 0){
            flag = true;
        }
        if (obj instanceof Map && ((Map) obj).isEmpty()){
            flag = true;
        }
        if (obj instanceof List && ((List) obj).size() == 0){
            flag = true;
        }
        return flag;
    }

    /**
     * 判断对象是否非空
     *
     * @param obj 对象
     * @return  非空
     */
    public static boolean isNotEmpty(Object obj) {
        return !isEmpty(obj);
    }

}
